package com.czr.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.czr.constant.isLogin;
import com.czr.dao.entity.CzrPost;
import com.czr.dao.entity.CzrReply;
import com.czr.dao.service.CzrColumnService;
import com.czr.dao.service.CzrLandscapeService;
import com.czr.dao.service.CzrPostService;
import com.czr.dao.service.CzrReplyService;
import com.czr.dto.*;
import com.czr.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@Api(value = "互动交流", tags = "互动交流")
@RestController
@RequestMapping("/exchange")
public class ExchangeController {

    @Autowired
    private CzrColumnService czrColumnService;

    @Autowired
    private CzrLandscapeService czrLandscapeService;

    @Autowired
    private CzrPostService czrPostService;

    @Autowired
    private CzrReplyService czrReplyService;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @isLogin
    @ApiOperation(value = "栏目字典", notes = "栏目字典")
    @GetMapping("/dictionary")
    public Result<Object> dictionary() {
        try {
            return czrColumnService.dictionary();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @isLogin
    @ApiOperation(value = "栏目信息统计（文章数、评论数、点赞数）", notes = "栏目信息统计（文章数、评论数、点赞数）")
    @GetMapping("/statistics")
    public Result<Object> statistics(@RequestParam Integer columuId) {
        try {
            return czrColumnService.statistics(columuId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @isLogin
    @ApiOperation(value = "发帖排行版", notes = "发帖排行版")
    @GetMapping("/postRanking")
    public Result<Object> postRanking(@RequestParam Integer columuId) {
        try {
            return czrColumnService.postRanking(columuId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @isLogin
    @ApiOperation(value = "最新评论", notes = "最新评论")
    @GetMapping("/latestComments")
    public Result<Object> latestComments(@RequestParam Integer columuId) {
        try {
            return czrColumnService.latestComments(columuId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }

    @ApiOperation(value = "管理端-修改栏目信息", notes = "管理端-修改栏目信息")
    @PostMapping("/updateColumn")
    public Result<Object> updateColumn(@RequestBody String param) {
        try {
            return czrColumnService.updateColumn(param);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }

    @ApiOperation(value = "发帖", notes = "发帖")
    @PostMapping("/czrPost")
    public Result<Object> czrPost(@RequestBody CzrPostFbParam czrPostFbParam, HttpServletRequest request) {
        try {
            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=valueOperations.get(request.getHeader("token"));

            if(StringUtils.isEmpty(czrPostFbParam.getTitle())){
                return Result.success(201,"标题不能为空！");
            }else{
                if(czrPostFbParam.getTitle().length()>60){
                    return Result.success(201,"标题不能超过30个字！");
                }
            }

            if (!StringUtils.isEmpty(object)) {
//                JSONObject jsonObject=JSONObject.parseObject(object.toString());
//                if(jsonObject.getInteger("user_level")==0){
//                    return Result.success(201,"您的账号正在审核中，请审核通过后进行操作！");
//                }
                JSONObject cacheToken = JSON.parseObject(object.toString());
                CzrPost czrPost=new CzrPost();
                BeanUtils.copyProperties(czrPostFbParam, czrPost);
                czrPost.setUserId(cacheToken.getLong("id"));
                Result<Object> result = czrPostService.czrPost(czrPost);
                return result;
            } else {
                return Result.success(202,"请登录后进行发帖！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
    @isLogin
    @ApiOperation(value = "增加阅读量", notes = "增加阅读量")
    @ApiImplicitParams({
            //参数效验
            @ApiImplicitParam(name="postId",value="帖子id",required=true,dataType="Integer")
    })
    @GetMapping("/addReading")
    public Result<Object> addReading(@RequestParam Integer postId, HttpServletRequest request) {
        try {
            Result<Object> result = czrPostService.addReading(postId);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
    @isLogin
    @ApiOperation(value = "管理端-发帖审核", notes = "发帖审核")
    @ApiImplicitParams({
            //参数效验
            @ApiImplicitParam(name="postId",value="帖子id",required=true,dataType="Integer"),
            @ApiImplicitParam(name="status",value="状态 -1删除  1审核完成",required=true,dataType="Integer")
    })
    @GetMapping("/auditing")
    public Result<Object> auditing(@RequestParam Integer postId,@RequestParam Integer status, HttpServletRequest request) {
        try {
            Result<Object> result = czrPostService.auditing(postId,status);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
    @ApiOperation(value = "点赞", notes = "点赞")
    @ApiImplicitParams({
            //参数效验
            @ApiImplicitParam(name="likeId",value="帖子id或回复id，看情况传值",required=true,dataType="Integer"),
            @ApiImplicitParam(name="type",value="类型 0 帖子 1回复",required=true,dataType="Integer")
    })
    @GetMapping("/addLike")
    public Result<Object> addLike(@RequestParam Integer likeId,Integer type,HttpServletRequest request) {
        try {
            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=valueOperations.get(request.getHeader("token"));
            if (!StringUtils.isEmpty(object)) {
                JSONObject cacheToken = JSON.parseObject(object.toString());
                Result<Object> result = czrPostService.addLike(likeId,type,Integer.parseInt(cacheToken.get("id").toString()));
                return result;
            } else {
                return Result.success(202,"请登录后进行发帖！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @isLogin
    @ApiOperation(value = "帖子列表", notes = "帖子列表")
    @PostMapping("/list")
    public Result<Object> list(@RequestBody CzrPostParam czrPostParam,HttpServletRequest request) {
        try {

            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=null;
            if(!StringUtils.isEmpty(request.getHeader("token"))){
                 object=valueOperations.get(request.getHeader("token"));
            }
            if (!StringUtils.isEmpty(object)) {
                JSONObject cacheToken = JSON.parseObject(object.toString());
                Result<Object> result = czrPostService.list(czrPostParam,Integer.parseInt(cacheToken.get("id").toString()));
                return result;
            } else {
                Result<Object> result = czrPostService.list(czrPostParam,null);
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @isLogin
    @ApiOperation(value = "帖子详情", notes = "帖子详情")
    @GetMapping("/getInfo")
    public Result<Object> getInfo(@RequestParam Integer id) {
        try {
            Result<Object> result = czrPostService.getInfo(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @ApiOperation(value = "管理端-帖子列表", notes = "管理端-帖子列表")
    @PostMapping("/glList")
    public Result<Object> glList(@RequestBody CzrPostParam czrPostParam,HttpServletRequest request) {
        try {

            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=null;
            if(!StringUtils.isEmpty(request.getHeader("token"))){
                object=valueOperations.get(request.getHeader("token"));
            }
            if (!StringUtils.isEmpty(object)) {
                JSONObject cacheToken = JSON.parseObject(object.toString());
                Result<Object> result = czrPostService.glList(czrPostParam,Integer.parseInt(cacheToken.get("id").toString()));
                return result;
            } else {
                Result<Object> result = czrPostService.glList(czrPostParam,null);
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }
    @ApiOperation(value = "回帖", notes = "回帖")
    @PostMapping("/czrReply")
    public Result<Object> czrReply(@RequestBody CzrReplyParam czrReplyParam, HttpServletRequest request) {
        try {
            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=valueOperations.get(request.getHeader("token"));
            if (!StringUtils.isEmpty(object)) {
                JSONObject cacheToken = JSON.parseObject(object.toString());
                if(cacheToken.getInteger("user_level")==0){
                    return Result.success(201,"您的账号正在审核中，请审核通过后进行操作！");
                }
                CzrReply czrReply=new CzrReply();
                BeanUtils.copyProperties(czrReplyParam, czrReply);
                czrReply.setUserId(cacheToken.getInteger("id"));
                Result<Object> result = czrReplyService.czrReply(czrReply);
                return result;
            } else {
                return Result.success(202,"请登录后进行发帖！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @isLogin
    @ApiOperation(value = "查看评论", notes = "查看评论")
    @PostMapping("/replyList")
    public Result<Object> replyList(@RequestBody ReplyListParam replyListParam, HttpServletRequest request) {
        try {
            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=valueOperations.get(request.getHeader("token"));
            if (!StringUtils.isEmpty(object)) {
                JSONObject cacheToken = JSON.parseObject(object.toString());
                Result<Object> result = czrReplyService.replyList(replyListParam,Integer.parseInt(cacheToken.get("id").toString()));
                return result;
            } else {
                Result<Object> result = czrReplyService.replyList(replyListParam,null);
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }

    @ApiOperation(value = "删帖", notes = "删帖")
    @PostMapping("/delPost")
    public Result<Object> delPost(@RequestBody DelPostParam delPostParam, HttpServletRequest request) {
        try {
            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=valueOperations.get(request.getHeader("token"));
            if (!StringUtils.isEmpty(object)) {
//                JSONObject jsonObject=JSONObject.parseObject(object.toString());
//                if(jsonObject.getInteger("user_level")==0){
//                    return Result.success(201,"您的账号正在审核中，请审核通过后进行操作！");
//                }
                JSONObject cacheToken = JSON.parseObject(object.toString());
                Result<Object> result = czrPostService.delPost(delPostParam,cacheToken);
                return result;
            } else {
                return Result.success(202,"请登录后进行删帖！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
    @ApiOperation(value = "删回复", notes = "删回复")
    @PostMapping("/delReply")
    public Result<Object> delReply(@RequestBody DelReplyParam delReplyParam, HttpServletRequest request) {
        try {
            ValueOperations<Object, Object> valueOperations = redisTemplate.opsForValue();
            Object object=valueOperations.get(request.getHeader("token"));
            if (!StringUtils.isEmpty(object)) {
                JSONObject jsonObject=JSONObject.parseObject(object.toString());
                if(jsonObject.getInteger("user_level")==0){
                    return Result.success(201,"您的账号正在审核中，请审核通过后进行操作！");
                }
                JSONObject cacheToken = JSON.parseObject(object.toString());
                Result<Object> result = czrReplyService.delReply(delReplyParam,cacheToken);
                return result;
            } else {
                return Result.success(202,"请登录后进行删回复！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @isLogin
    @ApiOperation(value = "风景名胜", notes = "风景名胜")
    @GetMapping("/landscape")
    public Result<Object> landscape() {
        try {
            return czrLandscapeService.dictionary();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }

    @ApiOperation(value = "管理端-修改风景名胜", notes = "管理端-修改风景名胜")
    @PostMapping("/updateLandscape")
    public Result<Object> updateLandscape(@RequestBody String param) {
        try {
            return czrLandscapeService.updateLandscape(param);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }

}
